﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.BLL;
using System.Net;
using SalePartial.Utility;
using System.IO;
using System.Text;
using System.Xml;
using SalePartial.BLL.SAServiceRef;
using SalePartial.BLL;

public partial class AddVend : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int eptid = 0;
        if (!IsPostBack)
        {
            if (Request["appid"] == null ||
                Request["checkKey"] == null ||
                Request["Eptid"] == null)
            {
                SABLL.ShowInfoMsg(this, @"传入的参数不正确！");
                return;

            }
            else
            {
                //检查appid,checkKey的正确性，暂时不做
                

                //1.取得Eptid的值
                int.TryParse(Request["Eptid"].ToString(),out eptid);
            }

            //2.调用资源D
            string urlD = string.Format(GlobalSetting.SpxGetVendInfoUrl, GlobalSetting.SpxAppid, GlobalSetting.SpxCheckKey, eptid);
            string vendinfo = ExtractWebData(urlD);
            if (vendinfo != null)
            {
                //XML解析
                XmlDocument xmldoc = new XmlDocument();
                System.IO.StringReader xmlSR = new System.IO.StringReader(vendinfo);
                xmldoc.LoadXml(vendinfo);

                //获取节点列表
                XmlNode root = xmldoc.SelectSingleNode("root");//指定一个节点

                XmlNodeList topM = root.SelectNodes("OneItem");//获取节点下所有直接子节点
                foreach (XmlElement element in topM)
                {
                    string etpId = element.GetElementsByTagName("etpid")[0].InnerText;
                    string etpFullName = element.GetElementsByTagName("EtpFullName")[0].InnerText;
                    string etpLogoUrl = element.GetElementsByTagName("EtpLogoUrl")[0].InnerText;
                    string etpDesc = element.GetElementsByTagName("EtpDesc")[0].InnerText;
                    string address = element.GetElementsByTagName("Address")[0].InnerText;
                    string tel = element.GetElementsByTagName("Tel")[0].InnerText;
                    string webSiteUrl = element.GetElementsByTagName("WebSiteUrl")[0].InnerText;
                    string md5key = element.GetElementsByTagName("MD5Key")[0].InnerText;



                    VendorInfo newVendorInfo = new VendorInfo();
                    newVendorInfo.Name = etpFullName;
                    newVendorInfo.Description = etpDesc;

                    bool ret = SABLL.AddVendor(newVendorInfo);//我现在还不确定数据库中包括哪些字段，所以还没有对数据库进行字段添加修改

                    if (!ret)
                    {
                        SABLL.ShowErrMsg(this, "该用户已加入零部件库，不能重复加入！");
                        return;

                    }
                    else
                    {
                        SABLL.ShowInfoMsg(this, "成功加入零部件库！");
                    }
                }
                

                //请插入到Vend数据库里
            }
        }
        
    }

    private static string ExtractWebData(String url)
    {
        try
        {
            //HttpWebRequest rGet = (HttpWebRequest)WebRequest.Create(url);

            HttpWebRequest rGet = HttpWebResponseUtility.CreateGetHttpResponse(url, new CookieContainer());
            WebResponse rSet = rGet.GetResponse();
            Stream s = rSet.GetResponseStream();
            StreamReader sr = new StreamReader(s);
            StringBuilder sb = new StringBuilder();
            String Str;

            while ((Str = sr.ReadLine()) != null)
            {
                sb.Append(Str + "\n");
            }

            sr.Close();
            s.Close();
            rSet.Close();

            return sb.ToString();
        }
        catch (Exception e)
        {
            return "";
        }
    }
}